/*
 * @Description: 描述信息
 * @Version: 版本号
 * @Author: xuebao
 * @Date: 2023-12-11 10:11:43
 * @LastEditors: xuebao
 * @LastEditTime: 2023-12-14 19:57:20
 */
/***
 *
 * 工具函数封装
 */

import { Menu } from "@/types/api"


//金钱
export const formatMoney = (num?: number | string) => {
  // parseFloat()转成数字
  // .toLocaleString()只针对的是数字
  if (!num) return '0.00'
  const a = parseFloat(num.toString())
  return a.toLocaleString('Zh-CN', { style: 'currency', currency: 'CNY' })
}


// 格式化数字
export const formatNum = (num?: number | string) => {
  if (!num) return 0
  const a = num.toString()
  if (a.indexOf('.') > -1) return a.replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
  return a.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
}


//格式化日期
export const toLocalData = (date?: Date, rule?: string) => {
  let curDate = new Date()
  if (date) curDate = date

  if (rule === 'yyyy-MM-dd') return curDate.toLocaleDateString().split('/').join('-')
  if (rule === "HH:mm:ss") return curDate.toLocaleTimeString().split(':').join('-')
  return curDate.toLocaleString().split('/').join('-')
}


export const formatDate = (date?: Date | string, rule?: string) => {
  let curDate = new Date()
  if (date instanceof Date) curDate = date
  else if (date) curDate = new Date(date)

  let fmt = rule || 'yyyy-MM-dd HH:mm:ss'
  fmt = fmt.replace(/(y+)/, curDate.getFullYear().toString())

  type OType = {
    [key: string]: number
  }

  const O: OType = {
    "M+": curDate.getMonth() + 1,
    "d+": curDate.getDate(),
    "H+": curDate.getHours(),
    "m+": curDate.getMinutes(),
    "s+": curDate.getSeconds()
  }

  for (const k in O) {
    fmt = fmt.replace(new RegExp(`(${k})`), O[k] > 9 ? O[k].toString() : '0' + O[k].toString())
  }
  return fmt
}


//用户状态转换

export const formatStatus = (state?: number) => {
  if (state == 1) return '在职'
  if (state = 2) return '试用期'
  if (state == 3) return '离职'
}


// 获取页面路径
export const getMenuPath = (list: Menu.MenuItem[]): string[] => {
  //接收一个数组类型的数据
  return list.reduce((result: string[], item: Menu.MenuItem) => {
    //针对有children 并且没有button的数据进行递归便利
    return result.concat(Array.isArray(item.children) && !item.buttons ? getMenuPath(item.children) : item.path + '')
  }, [])

}

//递归获取路由的对象
export const searchRoute: any = (path: string, routes: any = []) => {

  for (const item of routes) {
    //item是对象
    if (item.path === path) return item
    if (item.children) {
      const result = searchRoute(path, item.children)
      if (result) return result
    }
  }
  return ''
}


//加密格式化手机号
export const formatPhone = (phone?: number | string) => {
  if (!phone) return '-'
  const phone1 = phone.toString()
  return phone1.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}


//递归查找数的路径，从父->子
export const findTreeNode = (tree: Menu.MenuItem[], pathName: string, path: string[]): string[] => {
  if (!tree) return []
  for (const data of tree) {
    path.push(data.menuName)
    if (data.path === pathName) return path
    if (data.children?.length) {
      const list = findTreeNode(data.children, pathName, path)
      if (list?.length > 0) {
        return list
      }
    }

    path.pop()//删除
  }
  return []
}
